/*
 * 权限指令
 */
import { userStores } from 'stores/user-stores'

const userStore = userStores()

export default {
  name: 'auth',
  mounted: (el, binding) => {
    const { value } = binding
    const permissions = userStore.btns
    if (!(permissions instanceof Map)) return el.parentNode && el.parentNode.removeChild(el)
    if (permissions.get('*:*:*')) return true
    if (value && value instanceof Array && value.length) {
      const hasPermissions = value.some(permission => permissions.get(permission))
      if (!hasPermissions) el.parentNode && el.parentNode.removeChild(el)
    } else throw new Error('请设置操作权限标签值')
  }
}
export const isAuth = value => {
  const permissions = userStore.btns
  if (!(permissions instanceof Map)) return false
  if (permissions.get('*:*:*')) return true
  if (value && value instanceof Array && value.length) return value.some(permission => permissions.get(permission))
  return false
}